﻿using System;
using System.Diagnostics;
using Microsoft.Office.Interop.Outlook;
using Microsoft.TeamFoundation.Client;

namespace Outlook.WorkItems.Extension.Addin.Actions
{
    public class ShowBuildReportAction : IBowieAction
    {
        private readonly UserProperty _userProperty;

        public ShowBuildReportAction(UserProperty userProperty)
        {
            _userProperty = userProperty;
        }

        public void Do()
        {
            if (_userProperty == null || _userProperty.Value == null) 
                return;

            string value = _userProperty.Value.ToString();
            if (value.Contains("#"))
            {
                string[] values = value.Split('#');

                var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(values[0]));

                var tswa = tfs.GetService<TswaClientHyperlinkService>();
                if (tswa != null)
                {
                    var buildUrl = tswa.GetViewBuildDetailsUrl(new Uri(values[1]));
                    Process.Start(buildUrl.ToString());
                }
            }
        }
    }
}
